package com.gzz.orchid.context.domain.rbac.impl;

import com.gzz.orchid.common.core.util.I18nUtil;
import com.gzz.orchid.context.domain.rbac.IUserService;
import com.gzz.orchid.context.domain.rbac.entity.UserDo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author higmd
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UserService implements IUserService {
    private final UserRespository repository;

    @Override
    public void checkCodeUnique(UserDo userDo) throws IllegalArgumentException {
        UserDo exsitsDo = repository.findByCode(userDo.getUserName());
        if (Objects.nonNull(exsitsDo) && !exsitsDo.getId().equals(userDo.getId())) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("用户名[%s]已存在", userDo.getUserName()));
        }
    }

    @Override
    public void checkMobileUnique(UserDo userDo) throws IllegalArgumentException {
        UserDo exsitsDo = repository.findByCode(userDo.getUserName());
        if (Objects.nonNull(exsitsDo) && !exsitsDo.getId().equals(userDo.getId())) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("手机号码[%s]已存在", userDo.getUserName()));
        }
    }

    @Override
    public void checkEmailUnique(UserDo userDo) throws IllegalArgumentException {
        UserDo exsitsDo = repository.findByCode(userDo.getUserName());
        if (Objects.nonNull(exsitsDo) && !exsitsDo.getId().equals(userDo.getId())) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("邮箱[%s]已存在", userDo.getUserName()));
        }
    }
}
